﻿using Microsoft.Win32;
using OpenCvSharp;
using System.IO;
using System.Windows.Controls;

namespace ImageToAviMod.Views
{
    /// <summary>
    /// Interaction logic for ImgCAviMain
    /// </summary>
    public partial class ImgCAviMain : UserControl
    {
        public ImgCAviMain()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //VideoWriter videoWriter = new VideoWriter();
            var imagePath = "";
            var ofd = new OpenFolderDialog();
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == true)
            {
                imagePath = ofd.FolderName;
            }
            else
            {
                return;
            }
            var imageDir = new DirectoryInfo(imagePath);

            var images = imageDir.GetFiles("*.png");
            var file0 = images[0].FullName;

            Mat sourceMat = Cv2.ImRead(file0);
            Size imgSize = new Size(sourceMat.Width, sourceMat.Height);

            VideoWriter videoWriter = new VideoWriter("D:\\out.mp4", VideoWriter.FourCC(@"XVID"), 0.2, imgSize, true);

            for (int i = 0; i < images.Length; i++)
            {
                var fileName = $"{imagePath}\\{i}.png";

                if (File.Exists(fileName))
                {
                    Mat mat = new Mat();
                    string fileString = fileName;
                    mat = Cv2.ImRead(fileString);
                    if (!mat.Empty())
                    {
                        videoWriter.Write(mat);
                        Cv2.WaitKey(0);
                    }
                }
            }
        }
    }
}